<?php
class tag_db{
	var $sql;
	var $table = "jishigou_tag";
	var $mysql;

	function tag_db($mysql,$sql){
		$this->sql=$sql;
		$this->mysql=$mysql;
		$this->judge_sql();
	}

	function judge_sql(){
		$sql = trim($this->sql);

		if (preg_match ("/^\b(select)\b.*?\b(from)\b.*?(jishigou_tag).*?\b(where)\b/is", $sql)){
			$this->sql = $this->select($sql);
		}
		if (preg_match ("/^\b(insert)\b.*?(jishigou_tag).*?\b(values)\b.*?/is", $sql)){
			$this->sql = $this->insert($sql);
		}
		if (preg_match ("/^\b(update)\b.*?(jishigou_tag).*?\b(set)\b.*?\bwhere\b/is", $sql)){
			$this->sql = $this->update($sql);
		}
		if (preg_match ("/^\b(delete)\b.*?jishigou_tag.*?\bwhere\b.*?/is", $sql)){
			//			$this->sql = $this->delete($sql);
		}
	}

	function select($sql){
		if ($c=preg_match_all ("/(.*?)\b(from)\b(.*?)\b(where)\b(.*?)$/is", $sql, $matches)){
			$selsect=$matches[1][0];
			$from = $matches[3][0];
			$where=$matches[5][0];
			if(preg_match("/.*?\b(name)\b.*?(?:=|in).*?/is", $where)){
				$c_table = $this->select_name($selsect,$where);
			}elseif(preg_match("/.*?\b(id)\b.*?(?:=|in).*?/is", $where)){
				$c_table = $this->select_id($selsect,$where);
			}

			return $c_table;
		}

	}

	function select_id($selsect,$where){
		$tablename=$this->by_ids($where);
		foreach($tablename as $v){
			$table[]="(".$selsect." from ".$v ." where ".$where.")";
		}
		return implode(" union all ",$table);
	}
	function by_ids($where){
		$tablename[]=$this->table."_";
		if($c=preg_match_all ("/.*?\bid\b.*?in\s?\((.*?)\).*?/is", $where, $matches)){
			$ids=$matches[1][0];
			$_tabls="select * from ".TABLE_PREFIX."creatable where addid in(".$ids.")";
			$_info = $this->mysql->Query($_tabls);
			while ($row = $_info->GetRow()){
				$tablename[$row[key]]= $this->table."_".$row[key];
			}
		}elseif($c=preg_match_all ("/.*?\bid\b.*?=\s?[?:\"|\'](.*?)[?:\"|\'].*?/is", $where, $matches)){
			$id=$matches[1][0];
			$_info = $this->mysql->FetchFirst("select * from ".TABLE_PREFIX."creatable where addid = ".$id);
			$tablename[]= $this->table."_".$_info[key];
		}
		return $tablename;
	}


	function select_name($selsect,$where){
		$tablename=$this->by_name($where);
		foreach($tablename as $v){
			$table[]="(".$selsect." from ".$v ." where ".$where.")";
		}
		return implode(" union all ",$table);
	}
	function by_name($where){
		$tablename[]=$this->table."_";
		if($c=preg_match_all ("/.*?\bname\b.*?in\s?\((.*?)\).*?/is", $where, $matches)){
			$names=$matches[1][0];
			$names=split(",",$names);

			foreach($names as $v){
				if($c=preg_match_all ("/'?(.*?)'?$/is", $v, $ma)){
					$w = $ma[1][0];
					$name[]=$w;
					$key[]=$this->table_name($w);
					$this->creat_table($w);
				}
			}
			foreach($key as $v){
				$tablename[]= $this->table."_".$v;
			}

		}elseif($c=preg_match_all ("/.*?\bname\b.*?=\s?[?:\"|\'](.*?)[?:\"|\'].*?/is", $where, $matches)){
			$name=$matches[1][0];
			$key=$this->table_name($name);
			$this->creat_table($name);
			$tablename[]= $this->table."_".$key;
		}
		return $tablename;

	}

	function insert($sql){
		if ($c=preg_match_all ("/^\binsert\s{0,}into\b.*?\bjishigou_tag\b`{0,}(.*?)$/is", $sql, $matches)){
			$insert="insert into ";
			$values=$matches[1][0];

			if($c=preg_match_all ("/^\((.*?)\)\s?values\s?\((.*?)\).*?$/is", $values, $matches)){
				$filed=$matches[1][0];
				$filed=split(",",$filed);
				$value=$matches[2][0];
				$value=split(",",$value);
				$dateline=$last_post=$total_count=$user_count=$topic_count=$tag_count=$status=0;
				foreach($filed as $k=>$v){
					if(preg_match ("/\bname\b/is", $v)){$name=$value[$k];continue;}
					if(preg_match("/\buser_id\b/is", $v)){$user_id=$value[$k];continue; }
					if(preg_match ("/\busername\b/is", $v)){$username=$value[$k];continue;}
					if(preg_match ("/\bdateline\b/is", $v)){$dateline=$value[$k];continue;}
					if(preg_match ("/\blast_post\b/is", $v)){$last_post=$value[$k];continue;}
					if(preg_match ("/\btotal_count\b/is", $v)){$total_count=$value[$k];continue;}
					if(preg_match ("/\buser_count\b/is", $v)){$user_count=$value[$k];continue;}
					if(preg_match ("/\btopic_count\b/is", $v)){$topic_count=$value[$k];continue;}
					if(preg_match ("/\btag_count\b/is", $v)){$tag_count=$value[$k];continue;}
					if(preg_match ("/\bstatus\b/is", $v)){$status=$value[$k];continue;}
				}
				$value_sql="$name,$user_id,$username,$dateline,$last_post,$total_count,$user_count,$topic_count,$tag_count,$status";
			}elseif($c=preg_match_all ("/^values\s?\((.*?)\).*?$/is", $values, $matches)){
				$value=$matches[1][0];
				$value_sql=$value;
				$value=split(",",$value);
				$name=$value[0];
			}

			if($c=preg_match_all ('/[?:\"|\'](.*?)[?:\"|\']$/is', $name, $matches)){
				$w = $matches[1][0];
				$key=$this->table_name($w);
				$this->creat_table($w);
			}

			$_info = $this->mysql->FetchFirst("select max(addid) as addid from ".TABLE_PREFIX."creatable ");
			$addid = 0;
			if(!empty($_info[addid])){
				$addid = $_info[addid];
			}
			$addid = $addid+1;
			$_tabls="insert into  ".TABLE_PREFIX."creatable (`addid`,`key`)values($addid,'$key')";
			$q =$this->mysql->Query($_tabls);
			$value_sql =$addid.",".$value_sql;

			return $insert.$this->table."_".$key." (`id`,`name`,`user_id`, `username`,`dateline`,`last_post`,`total_count`,`user_count`,`topic_count`,`tag_count`,`status`)
		     values ($value_sql)";	

		}
	}

	function update($sql){
		if ($c=preg_match_all ("/^\bupdate\b.*?jishigou_tag.*?\bset\b(.*?)\bwhere\b(.*?)$/is", $sql, $matches)){
			$update="UPDATE ";
			$sets=$matches[1][0];
			$where=$matches[2][0];
			if(preg_match("/.*?\b(id)\b.*?/is", $where)){
				$tablename=$this->by_ids($where);
				foreach($tablename as $v){
					$up_sql=$update.$v." set ".$sets." where ".$where." ;";
					$q =$this->mysql->Query($up_sql);
				}
				return "select * from jishigou_tables where day ='201106'";
			}
		}
	}

	function delete($sql){

	}

	function table_name($name){
		return substr(md5($name),0,1);
	}
	function creat_table($name){
		$key=$this->table_name($name);
		$_tabls="select * from ".TABLE_PREFIX."creatable where `key` ='$key' limit 1";
		$_info = $this->mysql->FetchFirst($_tabls);

		if(empty($_info)){
			$_tabls="CREATE TABLE IF NOT EXISTS `".$this->table."_".$key."` (
              `id` mediumint(8) unsigned NOT NULL auto_increment,
              `name` char(30) NOT NULL default '',
              `user_id` mediumint(8) unsigned NOT NULL default '0',
              `username` char(15) NOT NULL default '',
              `dateline` int(10) unsigned NOT NULL default '0',
              `last_post` int(10) unsigned NOT NULL default '0',
               `total_count` int(10) unsigned NOT NULL default '0',
               `user_count` mediumint(8) unsigned NOT NULL default '0',
               `topic_count` mediumint(8) unsigned NOT NULL default '0',
               `tag_count` mediumint(8) NOT NULL default '0',
               `status` tinyint(4) NOT NULL default '0',
                PRIMARY KEY  (`id`),
                UNIQUE KEY `name` (`name`),
                KEY `total_count` (`total_count`),
                KEY `last_post` (`last_post`),
                KEY `user_id` (`user_id`)
              ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;";
			$q =$this->mysql->Query($_tabls);
		}

	}
}
?>